home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / bgiherc.zip / HERCULES.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-31  |  10KB  |  459 lines

  1. UNIT HERCULES;
  2. {  February 21, 1989  Turbo Pascal .TPU source                          }
  3. {  A collection of subroutines to mani[ulate the unique aspects of the  }
  4. {  Hercules family of video cards.  These are all character mode or     }
  5. {  InColor Card palette manipulation routines.  All mode changes        }
  6. {  assume a 9x14 character matrix.                                      }
  7.  
  8. INTERFACE
  9.  
  10. uses
  11.     dos;
  12.  
  13. type
  14.    ArrayOfPal  = array[0..15] of byte;
  15.    ArrayOfHPal = array[0..17] of byte;
  16.    Font        = array[0..4095] of byte;
  17.    ScrRec      = record
  18.                    CHR : char;
  19.                    ATR : byte;
  20.                  end;
  21.    FontMem     = array[0..11] of Font;
  22.    AdapterType = (None,MDA,Herc102,Herc112,Herc222,CGA,EGAMono,EGAColor,
  23.                   VGAMono,VGAColor,MCGAMono,MCGAColor);
  24.  
  25. var
  26.    AttBits    : byte;
  27.    PalBits    : byte;
  28.    CursorBits : byte;
  29.  
  30.  
  31. Procedure Set48K;
  32. Procedure Set4K;
  33. Procedure SetROM;
  34. Procedure Write48K(Text : string; AttValue, XPos, YPos : integer);
  35. Procedure SetNormalAtt;
  36. Procedure SetAlternateAtt;
  37. Procedure LoadPal(PalArray : ArrayOfPal);
  38. Procedure EnableIPal;
  39. Procedure DisableIPal;
  40. Procedure InitCursor(Start, Stop, Color : integer);
  41. Procedure InitOverStrike(Position, Color : integer);
  42. Procedure InitUnderScore(Position, Color : integer);
  43. Procedure ClearFonts;
  44. Procedure ResetVid;
  45. Procedure LoadHPAL;
  46. Procedure LoadHFNT;
  47. Function CheckVid : AdapterType;
  48. Function LoadFontFile(FileName : string; StartType, Planes : integer) : integer;
  49.  
  50.  
  51. IMPLEMENTATION
  52.  
  53.  
  54. Procedure Set48K;
  55.  
  56.  begin
  57.   port[$03B4] := $14;
  58.   port[$03B5] := $5;
  59.  end;
  60.  
  61.  
  62. Procedure Set4K;
  63.  
  64.  begin
  65.   port[$03B4] := $14;
  66.   port[$03B5] := $1;
  67.  end;
  68.  
  69.  
  70. Procedure SetROM;
  71.  
  72.  begin
  73.   port[$03B4] := $14;
  74.   port[$03B5] := $0;
  75.  end;
  76.  
  77.  
  78. Procedure Write48K(Text : string; AttValue, XPos, YPos : integer);
  79.  
  80.  var
  81.     i         : integer;
  82.     ScrPtr    : integer;
  83.     ScrollPtr : integer;
  84.     Screen    : array[0..1999] of ScrRec absolute $B000:0;
  85.  
  86.  begin
  87.   ScrPtr := ((XPos * 80) + YPos);
  88.   For i := 1 to Length(Text) do
  89.    begin
  90.     if ScrPtr = 2000
  91.      then
  92.       begin
  93.        For ScrollPtr := 0 to 1919 do
  94.          Screen[ScrollPtr] := Screen[ScrollPtr + 80];
  95.        ScrPtr := 1920;
  96.        For ScrollPtr := 1920 to 1999 do
  97.         begin
  98.          Screen[ScrollPtr].CHR := ' ';
  99.          Screen[ScrollPtr].ATR := Lo(AttValue);
  100.         end;
  101.       end;
  102.     Screen[ScrPtr].CHR := Text[i];
  103.     Screen[ScrPtr].ATR := Lo(AttValue);
  104.     ScrPtr := ScrPtr + 1;
  105.     port[$03B4] := $0E;
  106.     port[$03B5] := Hi(ScrPtr);
  107.     port[$03B4] := $0F;
  108.     port[$03B5] := Lo(ScrPtr);
  109.    end;
  110.  end;
  111.  
  112.  
  113. Procedure SetNormalAtt;
  114.  
  115.  begin
  116.   AttBits := $20;
  117.   port[$03B4] := $17;
  118.   port[$03B5] := AttBits OR PalBits OR CursorBits;
  119.  end;
  120.  
  121.  
  122. Procedure SetAlternateAtt;
  123.  
  124.  begin
  125.   AttBits := $00;
  126.   port[$03B4] := $17;
  127.   port[$03B5] := AttBits OR PalBits OR CursorBits;
  128.  end;
  129.  
  130.  
  131. Procedure LoadPal(PalArray : ArrayOfPal);
  132.  
  133.  var
  134.     ResetByte : byte;
  135.     i         : integer;
  136.  
  137.  begin
  138.   port[$03B4] := $1C;
  139.   ResetByte := port[$03B5];
  140.   For i := 0 to 15 do
  141.     port[$03B5] := PalArray[i];
  142.  end;
  143.  
  144.  
  145. Procedure EnableIPal;
  146.  
  147.  begin
  148.   PalBits := $10;
  149.   port[$03B4] := $17;
  150.   port[$03B5] := AttBits OR PalBits OR CursorBits;
  151.  end;
  152.  
  153.  
  154. Procedure DisableIPal;
  155.  
  156.  begin
  157.   PalBits := $00;
  158.   port[$03B4] := $17;
  159.   port[$03B5] := AttBits OR PalBits OR CursorBits;
  160.  end;
  161.  
  162.  
  163. Procedure InitCursor(Start, Stop, Color : integer);
  164.  
  165.  begin
  166.   CursorBits := Lo(Color);
  167.   port[$03B4] := $17;
  168.   port[$03B5] := AttBits OR PalBits OR CursorBits;
  169.   port[$03B4] := $0A;
  170.   port[$03B5] := Lo(Start);
  171.   port[$03B4] := $0B;
  172.   port[$03B5] := Lo(Stop);
  173.  end;
  174.  
  175.  
  176. Procedure InitOverStrike(Position, Color : integer);
  177.  
  178.  begin
  179.   port[$03B4] := $16;
  180.   port[$03B5] := (Lo(Color) SHL 4) OR Position;
  181.  end;
  182.  
  183.  
  184. Procedure InitUnderScore(Position, Color : integer);
  185.  
  186.  begin
  187.   port[$03B4] := $15;
  188.   port[$03B5] := (Lo(Color) SHL 4) OR Position;
  189.  end;
  190.  
  191.  
  192. Procedure ClearFonts;
  193.  
  194.  var
  195.     FontNo   : integer;
  196.     ScanLine : integer;
  197.     FontByte : FontMem absolute $B400:0;
  198.  
  199.  begin
  200.   port[$03B4] := $18;
  201.   port[$03B5] := $0F;
  202.   For FontNo := 0 to 11 do
  203.     For ScanLine := 0 to 4095 do
  204.       FontByte[FontNo, ScanLine] := 0;
  205.  end;
  206.  
  207.  
  208. Procedure ResetVid;
  209.  
  210.  var
  211.     i         : integer;
  212.     BlankChar : ScrRec;
  213.     Screen    : array[0..1999] of ScrRec;
  214.  
  215.  begin
  216.   AttBits    := $20;
  217.   PalBits    := $00;
  218.   CursorBits := $07;
  219.   SetROM;
  220.   SetNormalAtt;
  221.   DisableIPal;
  222.   InitCursor(12, 13, 7);
  223.   InitOverstrike(6, 7);
  224.   InitUnderScore(13, 7);
  225.   BlankChar.CHR := ' ';
  226.   BlankChar.ATR := 0;
  227.   For i := 0 to 1999 do
  228.     Screen[i] := BlankChar;
  229.  end;
  230.  
  231.  
  232. function GetEnvironmentString(SearchString : string) : string;
  233.     {-Return a string from the environment}
  234.   type
  235.     Env = array[0..32767] of Char;
  236.   var
  237.     EPtr : ^Env;
  238.     EStr : string;
  239.     EStrLen : Byte absolute EStr;
  240.     Done : Boolean;
  241.     SearchLen : Byte absolute SearchString;
  242.     I : Word;
  243.   begin
  244.     GetEnvironmentString := '';
  245.     if SearchString = '' then
  246.       Exit;
  247.  
  248.     {force upper case}
  249.     for I := 1 to SearchLen do
  250.       SearchString[I] := Upcase(SearchString[I]);
  251.  
  252.     EPtr := Ptr(MemW[PrefixSeg:$2C], 0);
  253.     I := 0;
  254.     if SearchString[SearchLen] <> '=' then
  255.       SearchString := SearchString+'=';
  256.     Done := False;
  257.     EStrLen := 0;
  258.     repeat
  259.       if EPtr^[I] = #0 then begin
  260.         if EPtr^[Succ(I)] = #0 then begin
  261.           Done := True;
  262.           if SearchString = '==' then begin
  263.             EStrLen := 0;
  264.             Inc(I, 4);
  265.             while EPtr^[I] <> #0 do begin
  266.               Inc(EStrLen);
  267.               EStr[EStrLen] := EPtr^[I];
  268.               Inc(I);
  269.             end;
  270.             GetEnvironmentString := EStr;
  271.           end;
  272.         end;
  273.         if Copy(EStr, 1, SearchLen) = SearchString then begin
  274.           GetEnvironmentString := Copy(EStr, Succ(SearchLen), 255);
  275.           Done := True;
  276.         end;
  277.         EStrLen := 0;
  278.       end
  279.       else begin
  280.         Inc(EStrLen);
  281.         EStr[EStrLen] := EPtr^[I];
  282.       end;
  283.       Inc(I);
  284.     until Done;
  285.   end;
  286.  
  287.  
  288.  
  289. Procedure LoadHPAL;
  290.  
  291.  var
  292.     ResetByte : byte;
  293.     i         : integer;
  294.     HPAL      : string;
  295.     ThePal    : ArrayOfHPal;
  296.     PALFile   : file of ArrayOfHPal;
  297.  
  298.  begin
  299.   HPAL := GetEnvironmentString('HPAL');
  300.   If HPAL <> ''
  301.    then
  302.     begin
  303.      assign(PALFile, HPAL);
  304.      {$I-};
  305.      reset(PALFile);
  306.      {$I+};
  307.      If IOResult = 0
  308.       then
  309.        begin
  310.         read(PALFile, ThePal);
  311.         port[$03B4] := $1C;
  312.         ResetByte := port[$03B5];
  313.         For i := 0 to 15 do
  314.           port[$03B5] := ThePal[i];
  315.         port[$03B4] := $17;
  316.         port[$03B5] := ThePal[16];
  317.         port[$03B4] := $15;
  318.         port[$03B5] := ThePal[17];
  319.        end;
  320.     end;
  321.  end;
  322.  
  323.  
  324. Procedure LoadHFNT;
  325.  
  326.  var
  327.     HFNT : string;
  328.     dummy : integer;
  329.  
  330.  begin
  331.   HFNT := GetEnvironmentString('HFNT');
  332.   If HFNT <> ''
  333.    then
  334.     begin
  335.      dummy := LoadFontFile(HFNT, 0, 0);
  336.      Set4K;
  337.     end;
  338.  end;
  339.  
  340.  
  341.  
  342. Function WhichHerc : AdapterType;
  343.  
  344. var
  345.    ReadPort      : byte;
  346.    QueryLoop     : integer;
  347.    RetraceToggle : integer;
  348.  
  349. begin
  350.  RetraceToggle := 0;
  351.  ReadPort := port[$03BA] AND $80;
  352.  For QueryLoop := 1 to 10000 do
  353.    If (port[$03BA] AND $80) <> ReadPort
  354.     then
  355.      begin
  356.       ReadPort := port[$03BA] AND $80;
  357.       RetraceToggle := RetraceToggle + 1;
  358.      end;
  359.  If RetraceToggle > 2
  360.   then
  361.    begin
  362.     ReadPort := port[$03BA] AND $70;
  363.     case ReadPort of
  364.      $10 : WhichHerc := Herc112;
  365.      $50 : WhichHerc := Herc222;
  366.      else WhichHerc := Herc102;
  367.     end
  368.    end
  369.   else WhichHerc := MDA;
  370. end;
  371.  
  372.  
  373. Function CheckVid : AdapterType;
  374.  
  375. var
  376.    Code : Byte;
  377.    Regs : Registers;
  378.  
  379. begin
  380.  Regs.AH := $1A;
  381.  Regs.AL := $00;
  382.  Intr($10, Regs);
  383.  If Regs.AL = $1A
  384.   then
  385.    begin
  386.     case Regs.BL of
  387.      $00 : CheckVid := None;
  388.      $01 : If WhichHerc = MDA
  389.             then CheckVid := MDA
  390.             else CheckVid := WhichHerc;
  391.      $02 : CheckV